import Sprite from '../base/sprite'
import nm from '../../sdk/index'
import { screenWidth, screenHeight } from '../../constant/index'
import Music from '../../store/music'
import { getCanvas } from '../../../ctx'
import { getCloudUrl } from '../../utils'


/**
 * 原来的尺寸
 * width: 590px
 * height: 423px
 */
export const BG_IMG_SRC_START = getCloudUrl('images/start-game-2.png')
export const BG_IMG_SRC_DOEN = getCloudUrl('images/start-game-1.png')
export const WIDYH = 147.5 * 1.5
export const HEIGHT = 106 * 1.5
export const BOTTOM = 150
/**
 * 游戏背景类
 */
export default class StartButton extends Sprite {
  constructor() {
    super(BG_IMG_SRC_START, WIDYH, HEIGHT)
    this.init()
  }
  init () {
    this.x = screenWidth / 2 - this.width / 2
    this.y = screenHeight - this.height - BOTTOM
    this.isDown = false
    this.start = getCanvas().createImage()
    this.start.src = BG_IMG_SRC_START
    this.down = getCanvas().createImage()
    this.down.src = BG_IMG_SRC_DOEN
    this.addEvent('touchend', (e) => {
      this.isDown = false
      nm.push('coins')
    })
    this.addEvent('touchstart', (e) => {
      this.isDown = true
      Music.createdAudio('StartGameBgm')
    })
  }
  update () {
    this.img = this.isDown ? this.down : this.start
  }
  render(ctx) {
    ctx.drawImage(
      this.img,
      this.x,
      this.y,
      this.width,
      this.height
    )
  }
}
